﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CRMAdminDashboard.CrmSvc;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace CRMAdminDashboard
{
    /// <summary>
    /// Base class for helpers
    /// </summary>
    public class HelperBase
    {

        #region Local Methods

        /// <summary>
        /// Save a serializable object in an XMLFile
        /// </summary>
        /// <param name="serializer">XML Serializer</param>
        /// <param name="serializableObject">Object to serialize</param>
        protected void SaveXmlFile(XmlSerializer serializer, object serializableObject,
            out string fileName)
        {
            fileName = string.Empty;
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
            dialog.OverwritePrompt = true;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                FileStream file = null;
                try
                {
                    // serialize into a XML document
                    XmlDocument xmlDoc = LoadXml(serializer, serializableObject);

                    // encode and save in the file
                    file = new FileStream(dialog.FileName, FileMode.Create);
                    XmlTextWriter xmlWriter = new XmlTextWriter(file, Encoding.UTF8);
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlDoc.WriteContentTo(xmlWriter);
                    fileName = dialog.FileName;
                }
                finally
                {
                    if (file != null)
                    {
                        file.Close();
                    }
                }
            }
        }

        /// <summary>
        /// Format XML with indentation
        /// </summary>
        protected string IndentXml(string xmlString)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlString);
            return FormatXml(xmlDoc);
        }

        /// <summary>
        /// Serialize and format XML with indentation
        /// </summary>
        protected string IndentXml(XmlSerializer serializer, object serializableObject)
        {
            XmlDocument xmlDoc = LoadXml(serializer, serializableObject);
            return FormatXml(xmlDoc);
        }

        /// <summary>
        /// Serialize into a XML document
        /// </summary>
        private XmlDocument LoadXml(XmlSerializer serializer, object serializableObject)
        {
            // serialize into a XML document
            using (StringWriter writer = new StringWriter())
            {
                serializer.Serialize(writer, serializableObject);
                writer.Flush();
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(writer.ToString());

                // force the encoding to be UTF-8
                if (xmlDoc.FirstChild.GetType() == typeof(XmlDeclaration))
                {
                    XmlDeclaration decl = (XmlDeclaration)xmlDoc.FirstChild;
                    decl.Encoding = "utf-8";
                }

                return xmlDoc;
            }
        }

        /// <summary>
        /// Pretty-print a XML doc
        /// </summary>
        private string FormatXml(XmlDocument xmlDoc)
        {
            MemoryStream memStream = null;
            XmlTextWriter xmlWriter = null;
            try
            {
                // save in string
                memStream = new MemoryStream();
                xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
                xmlWriter.Formatting = Formatting.Indented;
                xmlDoc.WriteContentTo(xmlWriter);
                xmlWriter.Flush();
                memStream.Flush();

                memStream.Position = 0;
                StreamReader streamReader = new StreamReader(memStream);

                String formattedXml = streamReader.ReadToEnd();

                return formattedXml;
            }
            finally
            {
                if (memStream != null)
                {
                    memStream.Close();
                }
                if (xmlWriter != null)
                {
                    xmlWriter.Close();
                }
            }
        }

        #endregion

    }
}
